package gui.drawers;

import dal.MapStruct;
import dal.object.MapObject;
import gui.GameConstants;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

@SuppressWarnings("serial")
public class RockDrawer extends Drawer {	
	public RockDrawer(MapStruct ms) {
		this.setOpaque(false);
		this.setSize(ms.getSize());
		
		dimension = ms.getDimension();
		vmo = ms.getRockVector();
		w = (int) dimension.getWidth(); 
		h = (int) dimension.getHeight();
	}
	
	// Drawing grounds
	protected void paintComponent(Graphics g) {
		if (img == null) {
			repaintAll();
		}
		
		g.drawImage(img, 0, 0, null);
	}
	
	/**
	 * deletes everything and repaints all again
	 */
	public void repaintAll() {
		img = new BufferedImage(getWidth(), getHeight(), BufferedImage.TRANSLUCENT);
		gr = img.getGraphics();
		
		location.x = 0;
		location.y = 0;
		
		// Setting MapObjects' location and drawing them
		for (int i = 0; i < h; i++) {
			if ((i & 1) == 1)
				location.x = GameConstants.picWidth50;
			else
				location.x = 0;
				
			for (int j = 0; j < dimension.getWidth(); j++) {
				MapObject mo = vmo.get(i * w + j);
				
				if (mo != null) {
					mo.setLocation(location);
					mo.drawObject(gr);
				}
				
				location.x += GameConstants.picWidth100;
			}
			location.y += GameConstants.picHeight75;
		}
	}
}